package rdbms;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import DAO.DatabasePool;
import DAO.ENSequenceDAO;


import model.DIConcept;
import model.ENSequence;

public class RDBMSDIConceptDAO {

	private String idConnection;
	private ENSequenceDAO sequence;
	private final String name = "CONCEPT";

	private static String INSERT = "insert into DI_CONCEPT(CONCEPT_ID, TERM_ID, CLASS_ID, SYNONYM_ID, VALID) values (?, ?, ?, ?, ?)";

	private static String DELETE = "delete from DI_CONCEPT where CONCEPT_ID = ?";

	private static String DELETE_ALL = "delete from DI_CONCEPT";

	private static String SELECT_BY_ID = "select TERM_ID, CLASS_ID, SYNONYM_ID, VALID "
			+ "from DI_CONCEPT where CONCEPT_ID = ?";

	private static String SELECT_ALL = "select * from DI_CONCEPT";

	private static String UPDATE = "update DI_CONCEPT set TERM_ID = ?, CLASS_ID = ?, SYNONYM_ID = ?, VALID = ? where CONCEPT_ID = ?";

	public RDBMSDIConceptDAO(final String idConnection, final ENSequenceDAO sequence) throws SQLException {
		this.idConnection = idConnection;
		this.sequence = sequence;
	}
	
	public boolean insert(DIConcept diConcept) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			diConcept.setConceptId(sequence.nextval(ENSequence.CONCEPT_ID));
			pst = conn.prepareStatement(INSERT);
			pst.setInt(1, diConcept.getConceptId());
			pst.setInt(2, diConcept.getTermId());
			pst.setInt(3, diConcept.getClassId());
			if(diConcept.getSynomymId() == 0){
				pst.setNull(4, java.sql.Types.INTEGER);
			}else{
				pst.setInt(4, diConcept.getSynomymId());
			}
			pst.setString(5, diConcept.getValid());
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while inserting "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" Insert - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" Insert - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public boolean delete(int id) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(DELETE);
			pst.setInt(1, id);
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while deleting "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" - Delete - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" - Delete - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public boolean deleteAll() throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(DELETE_ALL);
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while deleting(all) "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" - Delete(all) - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" - Delete(all) - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public DIConcept find(int id) throws Exception {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rss = null;
		DIConcept diConcept = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(SELECT_BY_ID);
			pst.setInt(1, id);
			rss = pst.executeQuery();
			if (rss.next()) {
				diConcept = new DIConcept(id, rss.getInt(1), rss.getInt(2), rss.getInt(3), rss.getString(4));
			}
			return diConcept;
		} catch (Exception e) {
			throw new Exception("Exception while retrieving "+name, e);
		} finally {
			if (rss != null)
				rss.close();
			if (pst != null)
				pst.close();
		}
	}

	public boolean update(DIConcept diConcept) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(UPDATE);
			pst.setInt(1, diConcept.getTermId());
			pst.setInt(2, diConcept.getClassId());
			pst.setInt(3, diConcept.getSynomymId());
			pst.setString(4, diConcept.getValid());
			pst.setInt(5, diConcept.getConceptId());
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while updating "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" - Update - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" - Update - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}
	
	
	public List<DIConcept> list(int noi) throws Exception {
		List<DIConcept> list = null;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rss = null;
		int ctr = 0;
		try {
			conn = DatabasePool.getConnection(idConnection);
			list = new ArrayList<DIConcept>();
			pst = conn.prepareStatement(SELECT_ALL);
			rss = pst.executeQuery();
			while (rss.next()) {
				if (++ctr > noi)
					break;
				DIConcept diConcept = new DIConcept(rss.getInt(1), rss.getInt(2), rss.getInt(3), rss.getInt(4), rss.getString(4));
				list.add(diConcept);
			}
			return list;
		} catch (Exception e) {
			throw new Exception("Exception while retrieving "+name, e);
		} finally {
			if (rss != null)
				rss.close();
			if (pst != null)
				pst.close();
		}
	}
}
